import cv2
import numpy as np
from padding import Padding
from interpolation import Interpolation
from filter import Filter


class PyramidDecompose:
    def __init__(self, filter, interolation):
        self.filter = filter
        self.interolation = interolation
    
    def __call__(self, src, layers, stage, size):
        """
            PyramidDecompose
        """
        ret = []
        for _ in range(layers):
            si = size

            dsts = []
            for _ in range(stage):

                dst = self.filter(src, si, 1.0)

                dsts.append(dst)

                si = size + 2

            # h, w
            h, w = src.shape

            # downsample
            src = self.interolation(src, size=(h//2, w//2))

            ret.append(dsts)

        return ret


if __name__ == '__main__':
    SRC = cv2.imread('peppers-bw.bmp', cv2.IMREAD_GRAYSCALE)

    def show(winname, m):
        for i, im in enumerate(m):
            for j, jm in enumerate(im):
                cv2.imshow(winname + str(i) + str(j), jm)
                cv2.resizeWindow(winname, 512, 512)
                cv2.waitKey(1)

    imgs = PyramidDecompose(Filter(Padding('REPEATE'), 'MEAN'), Interpolation('NEAREST'))(SRC, 3, 3, 3)

    show('mean+nearest', imgs)

    imgs = PyramidDecompose(Filter(Padding('REPEATE'), 'GAUSS'), Interpolation('NEAREST'))(SRC, 3, 3, 3)

    show('gauss+nearest', imgs)

    imgs = PyramidDecompose(Filter(Padding('REPEATE'), 'MEAN'), Interpolation('BILINEAR'))(SRC, 3, 3, 3)

    show('mean+bilinear', imgs)

    imgs = PyramidDecompose(Filter(Padding('REPEATE'), 'GAUSS'), Interpolation('BILINEAR'))(SRC, 3, 3, 3)

    show('gauss+bilinear', imgs)

    imgs = PyramidDecompose(Filter(Padding('REPEATE'), 'MEAN'), Interpolation('BICUBIC'))(SRC, 3, 3, 3)

    show('mean+bicubic', imgs)

    imgs = PyramidDecompose(Filter(Padding('REPEATE'), 'GAUSS'), Interpolation('BICUBIC'))(SRC, 3, 3, 3)

    show('gauss+bicubic', imgs)

    cv2.waitKey(0)